package com.example.neutonote2.data.dao;

import androidx.lifecycle.LiveData;
import androidx.room.*;
import com.example.neutonote2.data.model.Note;
import com.example.neutonote2.data.model.NoteWithTags;

import java.util.Date;
import java.util.List;

/**
 * 笔记数据访问对象
 */
@Dao
public interface NoteDao {
    @Insert
    long insert(Note note);

    @Update
    void update(Note note);

    @Delete
    void delete(Note note);

    @Query("DELETE FROM notes WHERE id = :id")
    void deleteById(int id);

    @Query("SELECT * FROM notes ORDER BY modified_time DESC")
    LiveData<List<Note>> getAllNotes();

    @Query("SELECT * FROM notes WHERE id = :id")
    LiveData<Note> getNoteById(int id);

    @Query("SELECT * FROM notes WHERE category = :category ORDER BY modified_time DESC")
    LiveData<List<Note>> getNotesByCategory(String category);

    @Query("SELECT * FROM notes WHERE title LIKE '%' || :query || '%' OR content LIKE '%' || :query || '%' ORDER BY modified_time DESC")
    LiveData<List<Note>> searchNotes(String query);
    
    @Query("SELECT * FROM notes WHERE next_review_date <= :currentDate AND review_status != '已掌握' ORDER BY next_review_date ASC")
    LiveData<List<Note>> getNotesToReview(Date currentDate);
    
    @Transaction
    @Query("SELECT * FROM notes WHERE id = :noteId")
    LiveData<NoteWithTags> getNoteWithTags(int noteId);
    
    @Transaction
    @Query("SELECT * FROM notes ORDER BY modified_time DESC")
    LiveData<List<NoteWithTags>> getAllNotesWithTags();
    
    @Transaction
    @Query("SELECT * FROM notes WHERE category = :category ORDER BY modified_time DESC")
    LiveData<List<NoteWithTags>> getNotesWithTagsByCategory(String category);
    
    @Transaction
    @Query("SELECT * FROM notes WHERE id IN (:noteIds) ORDER BY modified_time DESC")
    LiveData<List<NoteWithTags>> getNotesWithTagsByIds(List<Integer> noteIds);
} 